package com.fangshuoit.web.controller;

import com.fangshuoit.exception.UserNotExistException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Fangshuo_zhou on 2018/1/31.
 */
//@ControllerAdvice用来处理异常
@ControllerAdvice
public class ControllerExceptionHandler {

    @ExceptionHandler(UserNotExistException.class)
    @ResponseBody
    //返回时状态码为500，服务器内部错误
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Map<String, Object> hadlerUserNotExistException(UserNotExistException ex) {
        Map<String, Object> result = new HashMap();
        result.put("id", ex.getId());
        result.put("message", ex.getMessage());
        return result;
    }
}
